{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "author by zhenlei xu,hadxu123@gmail.com\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tensorflow.python.framework.ops.Tensor'>\n"
     ]
    }
   ],
   "source": [
    "hello = tf.constant('hello,hadxu')\n",
    "print type(hello)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "In order to make things happen,we need 'session'\n",
    "\"\"\"\n",
    "sess = tf.Session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'str'>\n",
      "<class 'tensorflow.python.framework.ops.Tensor'>\n",
      "hello,hadxu\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "run session\n",
    "\"\"\"\n",
    "hello_put = sess.run(hello)\n",
    "print type(hello_put)\n",
    "print type(hello)\n",
    "print hello_put"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " <class 'tensorflow.python.framework.ops.Tensor'> <class 'tensorflow.python.framework.ops.Tensor'>\n",
      "<type 'numpy.float32'> <type 'numpy.float32'>\n",
      "1.5 2.5\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "There are other types as well\n",
    "    Constant types\n",
    "    Operators\n",
    "    Variables\n",
    "    Placeholder\n",
    "\"\"\"\n",
    "a = tf.constant(1.5)\n",
    "b = tf.constant(2.5)\n",
    "print type(a),type(b)\n",
    "a_out = sess.run(a)\n",
    "b_out = sess.run(b)\n",
    "print type(a_out),type(b_out)\n",
    "print a_out,b_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tensorflow.python.framework.ops.Tensor'>\n",
      "4.0\n",
      "-------------------------------\n",
      "<class 'tensorflow.python.framework.ops.Tensor'>\n",
      "3.75\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Operation are also tf variables\n",
    "\"\"\"\n",
    "\n",
    "add = tf.add(a,b)\n",
    "print type(add)\n",
    "add_out = sess.run(add)\n",
    "print add_out\n",
    "print '-------------------------------'\n",
    "mul = tf.mul(a,b)\n",
    "print type(mul)\n",
    "mul_out = sess.run(mul)\n",
    "print mul_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tensorflow.python.ops.variables.Variable'>\n",
      "<class 'tensorflow.python.ops.variables.Variable'>\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Variables & placeholder\n",
    "\"\"\"\n",
    "X = np.random.rand(1,20)\n",
    "Input = tf.placeholder(tf.float32,[None,20])\n",
    "Weight = tf.Variable(tf.random_normal([20,10],stddev=0.5))\n",
    "Bias = tf.Variable(tf.zeros([1,10]))\n",
    "print type(Weight)\n",
    "print type(Bias)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------\n",
      "<class 'tensorflow.python.ops.variables.Variable'>\n",
      "<class 'tensorflow.python.ops.variables.Variable'>\n",
      "[[ 0.48615739 -0.07964883 -0.19031641  0.04079578 -0.50777459  0.90968579\n",
      "   0.43806839 -0.16237505 -0.05768506 -0.7283721 ]\n",
      " [-0.5649755   0.99307823 -0.10054398 -0.17644864 -0.05455542 -0.08488857\n",
      "   0.53352898 -0.42018485 -0.04191568 -0.18583851]\n",
      " [ 0.74560171 -0.07900789 -0.19514947  0.50605482  0.63347763 -0.82356244\n",
      "   0.06574752  0.26130351 -0.86578143 -0.55469775]\n",
      " [ 0.68748945 -0.32135105 -0.03816238  0.20410226 -0.16130997 -0.06836268\n",
      "   0.0353713   0.41791472 -0.63539398  0.55024958]\n",
      " [ 0.30872825  0.22658473 -1.02019191 -0.40133476  0.23219541  0.27412036\n",
      "   0.4332107  -0.30648983  0.68958902 -0.52128398]\n",
      " [-0.24847224 -0.5753563  -0.48788074  0.18904948 -1.03282142 -0.69544148\n",
      "  -0.05306473  0.15877773  0.22699043  1.02970457]\n",
      " [-0.26327935  0.73602372  0.78115094 -0.28752443  0.52254683 -0.50165236\n",
      "   0.82681763  0.04414455  0.14263582  0.09056416]\n",
      " [-0.54116333  1.28627717  0.00308081  0.66469413  0.37102887  0.30351964\n",
      "  -0.43768698  0.14652301 -0.07988074  0.28533927]\n",
      " [ 0.08062001 -0.08120023 -0.92034751 -0.40997127  0.25911996  0.34526798\n",
      "  -0.28680572 -0.33204028 -0.23506314  1.1614908 ]\n",
      " [ 0.53990114 -0.10134833  0.44015244 -0.26844484 -0.1930276  -0.3826575\n",
      "  -0.00368061  0.33935484 -0.21160309 -0.31620824]\n",
      " [-0.13100265  0.87689704 -0.26263309 -0.10545006 -0.45294413  0.06637911\n",
      "   1.05992508 -0.42957976 -0.00702196  0.22948021]\n",
      " [-0.18234435 -0.14309616 -0.82514334 -0.28797442  0.4217464  -0.16551347\n",
      "   0.2093284   1.43706417 -1.07954967 -0.41344294]\n",
      " [-0.25103068  0.40643001  0.30137715  0.04085376  0.04756845  0.91837406\n",
      "  -0.14864637  0.39266419 -0.14722636  0.03159926]\n",
      " [-0.249644   -0.097972    0.38592863 -0.49426144  0.29195711  0.13658667\n",
      "   0.24404919  0.578987    0.06419653 -0.1529832 ]\n",
      " [-0.29975924  0.39674634 -0.09926511 -0.26421419 -0.59089625  0.04578028\n",
      "   0.36856073 -0.52878416 -0.45778939  0.76392186]\n",
      " [ 0.10661642  0.56057763  0.60414606 -0.15978546 -0.43433881 -0.22435491\n",
      "   0.33784929  0.07076319  0.56607771 -0.62019026]\n",
      " [ 0.96326011 -1.71437359 -0.36492136 -0.11960912 -0.5422231  -0.49146017\n",
      "  -0.3755264   0.37543029 -0.02012958  0.13314974]\n",
      " [-0.00206998 -0.64343542  0.04813091 -0.53063053 -0.22245739  0.32240117\n",
      "  -0.28481868 -0.61421943  0.00679715  0.09087194]\n",
      " [ 0.2582587   0.10474217 -0.00530179  0.00916624  0.1986977   0.44699267\n",
      "   0.04210718  0.74085927 -0.04905055 -0.43694085]\n",
      " [ 0.07945849  0.01234039 -0.4352667  -0.57556206 -0.76992458  0.05523057\n",
      "   0.68055475  0.81815249 -0.2596646  -0.00351973]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Initialize Variables\n",
    "\"\"\"\n",
    "print '-----------------------'\n",
    "init = tf.initialize_all_variables()\n",
    "sess.run(init)\n",
    "print type(Weight)\n",
    "print type(Bias)\n",
    "print Weight.eval(sess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tensorflow.python.framework.ops.Tensor'>\n",
      "<type 'numpy.ndarray'>\n",
      "val is [[ 1.47093964  0.22816002 -1.17693925 -1.4436034  -0.16077709  0.62061214\n",
      "   1.39412796  1.74525023 -1.66221535 -1.14787364]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Operation with Variables and Placeholder\n",
    "\"\"\"\n",
    "oper = tf.matmul(Input,Weight)+Bias\n",
    "val = sess.run(oper,feed_dict={Input:X})\n",
    "print type(oper)\n",
    "print type(val)\n",
    "print \"val is\",val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------\n",
      "11.0 <type 'numpy.float32'>\n",
      "30.0 <type 'numpy.float32'>\n",
      "---------------------------------------\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Operator with Placeholder\n",
    "(This is very important)\n",
    "\"\"\"\n",
    "print '-----------------------------------'\n",
    "x = tf.placeholder(tf.float32)\n",
    "y = tf.placeholder(tf.float32)\n",
    "add = tf.add(x,y)\n",
    "mul = tf.mul(x,y)\n",
    "add_out = sess.run(add,feed_dict={x:5.0,y:6.0})\n",
    "mul_out = sess.run(mul,feed_dict={x:5.0,y:6.0})\n",
    "print add_out,type(add_out)\n",
    "print mul_out,type(mul_out)\n",
    "print '---------------------------------------'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "This is very excited!!!!!!!!!!!!\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
